﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Telerik.Windows.Controls;
using System.Collections.Generic;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;
using System.Windows.Controls;
using Baosight.ZNH.ZMCF.UI.ChildWindows;
using Baosight.ZNH.ZMCF.UI.Utils;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCF.UI.Download;

namespace Baosight.ZNH.ZMCF.UI
{
    /// <summary>
    /// 业务联系单的接收部门
    /// </summary>
    /// Title:ZMCC11
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 张志芳
    /// Version: 1.0
    /// History:
    ///       2011-06-28  张志芳 [创建]
    ///
    public partial class ZMCF27 : EFViewCmes
    {
        public ZMCF27()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            this.LayoutUpdated += new EventHandler(ZMCF27_LayoutUpdated);
        }

        /// <summary>
        /// 根据主表的状态设置按钮是否可见
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ZMCF27_LayoutUpdated(object sender, EventArgs e)
        {
            if (this.fdStatus.Value == null)
            {
                return;
            }
            if (this.fdStatus.Value.Equals("已发送"))
            {
                Button insert = this.FindName("INSERT") as Button;
                if (insert != null)
                {
                    insert.IsEnabled = false;
                }
                Button delete = this.FindName("DELETE") as Button;
                if (delete != null)
                {
                    delete.IsEnabled = false;
                }
            }
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击查看按钮执行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "反馈信息-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 主表新增操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "添加接收部门", "insert", null, sender, e);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnDeleteButtonClick(sender, e);
        }
       
        /// <summary>
        /// 从表弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新从表Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 从表新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.constructionUnit"));
            EFViewCmes view = null;
            if(_dialogType.Equals("insert"))
            {
                view = new ContactDepartment();
                view.EIDataContext = new Platform.Core.UI.EIDataContext();
                view.EIDataContext.Update(_eIInfo);
                (view as ContactDepartment).fdContactlistId.Value = this.fdContactlistId.Value;
            }
            else if (_dialogType.Equals("detail"))
            {
                view = new ContactSendDetails(_dialogType);
                view.EIDataContext = new Platform.Core.UI.EIDataContext();
                view.EIDataContext.Update(_eIInfo);
            }

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = view;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

    }
}
